package com.weimin.app3.controller;

import com.weimin.app3.config.samenameservice.FirstRequestInterceptor;
import com.weimin.app3.config.samenameservice.SecondRequestInterceptor;
import com.weimin.app3.config.samenameservice.ThirdRequestInterceptor;
import com.weimin.app3.feign.FirstApp1FeignClient;
import com.weimin.app3.feign.SecondApp1FeignClient;
import com.weimin.app3.feign.ThirdApp1FeignClient;
import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.springframework.cloud.netflix.feign.FeignClientsConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Import(FeignClientsConfiguration.class)
public class IndexController {

    private FirstApp1FeignClient firstApp1FeignClient;
    private SecondApp1FeignClient secondApp1FeignClient;
    private ThirdApp1FeignClient thirdApp1FeignClient;

    // https://cloud.spring.io/spring-cloud-openfeign/reference/html/#creating-feign-clients-manually
    public IndexController(Decoder decoder, Encoder encoder, Client client, Contract contract){
        this.firstApp1FeignClient = Feign.builder().client(client)
                .encoder(encoder)
                .decoder(decoder)
                .contract(contract)
                .requestInterceptor(new FirstRequestInterceptor())
                .target(FirstApp1FeignClient.class, "http://app1");

        this.secondApp1FeignClient = Feign.builder().client(client)
                .encoder(encoder)
                .decoder(decoder)
                .contract(contract)
                .requestInterceptor(new SecondRequestInterceptor())
                .target(SecondApp1FeignClient.class, "http://app1");

        this.thirdApp1FeignClient = Feign.builder().client(client)
                .encoder(encoder)
                .decoder(decoder)
                .contract(contract)
                .requestInterceptor(new ThirdRequestInterceptor())
                .target(ThirdApp1FeignClient.class, "http://app1");
    }

    @GetMapping("/first")
    @ResponseBody
    public String first() {
        return firstApp1FeignClient.hello();
    }

    @GetMapping("/second")
    @ResponseBody
    public String second() {
        return secondApp1FeignClient.hello();
    }

    @GetMapping("/third")
    @ResponseBody
    public String third() {
        return thirdApp1FeignClient.hello();
    }


}
